#! /usr/bin/env pyc

import os
import sys
import shutil
import stat
import filecmp


###################################################################################
def trash(path):
	'''
	Move the file into trash bin. 'path' may be a file or a directory
	'''
	import os
	from win32com.shell import shell, shellcon

	if os.path.exists(path):
		ret	= shell.SHFileOperation((0, shellcon.FO_DELETE , path, None, shellcon.FOF_NOCONFIRMATION | shellcon.FOF_ALLOWUNDO | shellcon.FOF_NOERRORUI | shellcon.FOF_SILENT))
		return ret[0]==0

	return False

############################################################################
def overwrite(src, dst):
	'''
	src should be a file or directory, and should exist
	dst should also be a path, may exist or not exist
	'''
	assert isinstance(src, str), 'src should be a string'
	assert isinstance(dst, str), 'dst should be a string'

	if not os.path.exists(src):
		raise ValueError('Can not find src= %s'%(src))

	# if src is a directory
	if os.path.isdir(src):
		if not os.path.exists(dst):
			os.makedirs(dst)

		assert os.path.exists(dst)
		for name in os.listdir(src):
			srcpath	= os.path.join(src, name)
			dstpath	= os.path.join(dst, name)

			overwrite(srcpath, dstpath)

	# if src is a file
	elif os.path.isfile(src):
		# if dst exists and they are not the same
		if os.path.exists(dst):
			assert os.path.isfile(dst), 'dst:%s should be a file'%(dst)
			if not filecmp.cmp(src, dst):
				os.chmod(dst, stat.S_IWRITE)
				shutil.copy(src, dst)

		# dst does not exist
		else:
			# to make sure directory of dst exists
			dirpath	= os.path.dirname(dst)
			if not os.path.exists(dirpath):
				os.makedirs(dirpath)
			# copy file
			shutil.copy(src, dst)
	else:
		assert False, 'should be dir or file'

###################################################################################
def install_win32():
	# firstly copy myself to should directory
	shoulddir	= r'D:\me\library\python\lib'
	if os.path.exists(shoulddir) and not __file__.startswith(shoulddir):
		srcdir	= os.path.dirname(__file__)
		trash(shoulddir)
		shutil.copytree(srcdir, shoulddir)

	# copy myself to import directory
	srcdir	= os.path.join(os.path.dirname(__file__), 'lib')
	dstdir	= os.path.join(sys.prefix, 'lib\\site-packages\\lib')				# c:\python31\Lib\site-packages\lib
	overwrite(srcdir, dstdir)

	# copy lib_third_party
	srcdir	= os.path.join(os.path.dirname(__file__), 'lib_third_party')
	dstdir	= os.path.join(sys.prefix, 'lib\\site-packages')					# c:\python31\Lib\site-packages
	
	overwrite(srcdir, dstdir)



###################################################################################
def install_linux2():
	srcdir		= os.path.join(os.path.dirname(__file__), 'lib')
	dstdir	= os.path.join(sys.prefix, 'lib/python3.1/dist-packages/lib')
	overwrite(srcdir, dstdir)	
	os.chmod(dstdir, 0o777)

###################################################################################
if __name__=='__main__':
	# exit on not win32 platforms
	if sys.platform == 'win32':
		install_win32()
	elif sys.platform == 'linux2':
		install_linux2()
	else:
		print('Not win32 or linux2 platforms')
		exit(0)
	
